﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using ContohWebAPI.Models;

namespace ContohWebAPI.Controllers
{
    public class MahasiswaController : ApiController
    {
        // GET api/<controller>
        public IQueryable<Mahasiswa> GetMahasiswa()
        {
            AkademikDataContext db = new AkademikDataContext();
            var data = db.Mahasiswas.Select(p => p);
            return data;
        }

        // GET api/<controller>/5
        public Mahasiswa GetMahasiswa(int nim)
        {
            AkademikDataContext db = new AkademikDataContext();
            Mahasiswa item = db.Mahasiswas.Where(p => p.NIM.Equals(nim)).Single<Mahasiswa>();

            return item;
        }

        // POST api/<controller>
        public void Post([FromBody]Mahasiswa mhs)
        {
            AkademikDataContext db = new AkademikDataContext();

            db.Mahasiswas.InsertOnSubmit(mhs);
            db.SubmitChanges();
        }

        // PUT api/<controller>/5
        public void Put(int nim, [FromBody]Mahasiswa mhs)
        {
            AkademikDataContext db = new AkademikDataContext();
            Mahasiswa item = db.Mahasiswas.Where(p => p.NIM.Equals(nim)).Single<Mahasiswa>();

            item.Nama = mhs.Nama;
            item.Alamat = mhs.Alamat;
            item.Umur = mhs.Umur;
            item.Jurusan = mhs.Jurusan;

            db.SubmitChanges();
        }

        // DELETE api/<controller>/5
        public void Delete(int nim)
        {
            AkademikDataContext db = new AkademikDataContext();
            Mahasiswa item = db.Mahasiswas.Where(p => p.NIM.Equals(nim)).Single<Mahasiswa>();
            db.Mahasiswas.DeleteOnSubmit(item);
            db.SubmitChanges();
        }
    }
}